# try:
from .path_generator import BlockUtil, ConnectorGenerator
# except:
#     from path_generator import BlockUtil, ConnectorGenerator

# try:
from .path_helper import *
# except:
#     from path_helper import *

from jxq.group import *
from OCC.Core.BRep import BRep_Builder
from OCC.Core.TopoDS import TopoDS_Compound


class ClampComposition():
    def __init__(self, points_pair, clamp, clamp_mseh, work_mesh, res, rps, *args, **kwargs):
        self.points_pair = points_pair
        self.clamp = clamp
        self.res = res
        self.rps = rps
        self.BG = BlockUtil(self.points_pair)
        self.CG = ConnectorGenerator(clamp_mseh, work_mesh, self.get_entry_exit(), res)

    @staticmethod
    def projection_distance(rps, start):
        vector1 = np.array(rps[:2])
        vector2 = np.array(start[:2])
        distance = np.linalg.norm(vector1 - vector2)
        return distance

    def get_maze(self):
        pass

    def get_entry_exit(self):
        entries = []
        exits = []
        for i in range(len(self.points_pair)):
            # if self.points_pair[i]["rps_type"]=="伸缩销":
            #     print("hello")
            entries.append(self.BG.get_entry(i))
            if self.points_pair[i]['target_axis'] is None:
                if self.res <= 2:
                    # maze = self.get_maze()
                    # voxel_matrix=maze.voxelized()
                    # coner = maze.get_bounds()[0]
                    # k=0
                    # temp = self.rps+self.points_pair[i]['target_normal']*k*self.res
                    # relative = temp - coner
                    # index = relative / self.res
                    # while voxel_matrix[int(index[0])][int(index[1])][int(index[2])]:
                    #     i=i+1
                    if isinstance(self.points_pair[i]['target_normal'], np.ndarray):
                        exits.append({"point": self.rps + self.points_pair[i]['target_normal'] * 3 * self.res})
                    elif isinstance(self.points_pair[i]['target_normal'], list):
                        exits.append(
                            {"point": [self.points_pair[i]['target'][j] + self.points_pair[i]['target_normal'][j] * 3 * self.res for j in range(len(self.points_pair[i]['target_normal']))],
                             "axis": self.points_pair[i]['target_normal'],
                             "rps_type": "connect"}
                            )
                else:
                    if isinstance(self.points_pair[i]['target_normal'], np.ndarray):
                        exits.append({"point": self.points_pair[i]['target'] + self.points_pair[i]['target_normal'] * 2 * self.res})
                    elif isinstance(self.points_pair[i]['target_normal'], list):
                        pass
            else:
                exits.append(self.BG.get_exit(i))
        return entries, exits

    def create_connecters(self):

        prism_list = self.CG.get_compound()
        return prism_list

    def get_connecters_compound(self):
        connecters = self.create_connecters()
        compound = TopoDS_Compound()
        B = BRep_Builder()
        B.MakeCompound(compound)

        for connecter in connecters:
            B.Add(compound, connecter)

        return compound

    def get_compound(self):
        connecters = self.create_connecters()
        compound = TopoDS_Compound()
        B = BRep_Builder()
        B.MakeCompound(compound)

        for connecter in connecters:
            B.Add(compound, connecter)
        if isinstance(self.clamp, Body):
            B.Add(compound, self.clamp.compound())
        elif isinstance(compound, TopoDS_Compound):
            B.Add(compound, self.clamp)

        return compound

    def show_paths(self):
        self.CG.show_paths()

    def get_connecter_dict(self):
        return self.CG.to_json()

    def to_json(self):
        ans = {}
        ans['version'] = "Eric"
        ans['PartInfo'] = self.get_connecter_dict()
        return ans
